package com.stlm2.controller;

import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.service.DdmGoodsService;
import com.stlm2.dems.service.GoodSpecificationService;
import com.stlm2.dems.entity.Specification;
import com.stlm2.dems.service.SpecificationService;
import com.stlm2.dems.service.SpecificationContentService;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/9/11
 * Description：crm 后台规格管理
 */
@Controller
@RequestMapping("/specificationC")
public class SpecificationController {

    @Resource
    private UserDao userDao;
    @Resource
    private SpecificationService specificationService;
    @Resource
    private SpecificationContentService specificationContentService;
    @Resource
    private GoodSpecificationService goodSpecificationService;
    @Resource
    private DdmGoodsService ddmGoodsService;

    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/specification/list");
        return modelAndView;
    }

    /**
     * Author：fuchunyu
     * Date：2017/9/11
     * Description：crm添加规格
     */

    @ResponseBody
    @RequestMapping(value = "addSpecification.json",produces="application/json;charset=UTF-8")
    public String addSpecification(String specificationName, String token, Integer goodid) {

        User user = userDao.getByToken(token);
        if (user == null)
            return CodeContent.returnLogin();
        if (StringUtils.isEmpty(specificationName))
            return CodeContent.returnFailure("请检查规格！");

        DdmGoods goods=ddmGoodsService.findEntityById(goodid);
        if (goods==null)
            return CodeContent.returnFailure("没有找到商品！");
        if(goods.getState())
            return CodeContent.returnFailure("请先下架商品再做修改！");

        Specification s1 = new Specification();
        s1.setCreateTime(new Date());
        s1.setCreateUserId(user.getId());
        s1.setGoodId(goodid);
        s1.setIsValid(true);
        s1.setName(specificationName);
        specificationService.addEntity(s1);

        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/9/11
     * Description：crm 获取规格下拉列表
     */

    @ResponseBody
    @RequestMapping(value = "/getSpecification.json",produces = "application/json;charset=UTF-8")
    public String getSpecification(Integer goodid){
        List<Specification> list=specificationService.getByGoodId(goodid);
        return CodeContent.returnSuccess(specificationService.convent(list));
    }

    @ResponseBody
    @RequestMapping(value = "/getSpecificationAndContent.json",produces="application/json;charset=UTF-8")
    public String getSpecificationAndContent(Integer goodid){

        List<Specification> specification=specificationService.getByGoodId(goodid);

        if(specification.size()>0){
            List<ModelMap> specifications=specificationService.convent(specification);
            for(ModelMap map:specifications){
                String id= map.get("id").toString();
                map.put("data",specificationContentService.convent(specificationContentService.getSpecificationContent(goodid,Integer.parseInt(id))));
            }
            return CodeContent.returnSuccess(specifications);
        }else {
            return CodeContent.returnSuccess(null);
        }
    }

    /**
     * Author：fuchunyu
     * Date：2017/9/11
     * Description：crm 获取规格
     */

    @ResponseBody
    @RequestMapping(value = "/getSpecificationAll.json",produces = "application/json;charset=UTF-8")
    public String getSpecificationAll(Integer goodid){
        List<Specification> list=specificationService.getByGoodId(goodid);
        return CodeContent.returnSuccess(specificationService.conventAll(list));
    }

    @ResponseBody
    @RequestMapping(value = "/detailsSpecification.json",produces = "application/json;charset=UTF-8")
    public String detailsSpecification(Integer id){
        Specification specification = specificationService.findEntityById(id);
        return CodeContent.returnSuccess(specificationService.builderModelAll(specification));
    }

    /**
     * Author：fuchunyu
     * Date：2017/9/11
     * Description：更新规格
     */

    @ResponseBody
    @RequestMapping(value = "/updateSpecification.json",produces = "application/json;charset=UTF-8")
    public String updateSpecification(String token,Integer specificationid,String name,String isValid){

        User user=userDao.getByToken(token);
        Map<String,String> map=new HashMap<String, String>();
        if(user==null)
           return CodeContent.returnSuccess(null);
        Specification specification = specificationService.findEntityById(specificationid);
        if(specification==null)
            return CodeContent.returnFailure("获取规格失败！");
        map.put("oldName",specification.getName());
        if(StringUtils.isNotEmpty(name))
            specification.setName(name);
        map.put("newName",name);
        if(StringUtils.isNotEmpty(isValid))
            specification.setIsValid(BooleanUtils.toBoolean(isValid));
        specificationService.updateEntity(specification);
        map.put("goodId",specification.getGoodId().toString());
        goodSpecificationService.updatePropertiesName(map);
        return CodeContent.returnSuccess(null);
    }
}